
//
//  HPToolBarView.swift
//  MyWeibo
//
//  Created by Apeng on 16/4/15.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit

class HPToolBarView: UIView {

    // 声明三个按钮
    var repostBtn: UIButton!
    var commentBtn: UIButton!
    var supportBtn: UIButton!
    
    
    // 拥有cellVM模型 绑定数据
    var statusCellVM: HPStatusCellViewModel? {
        
        didSet {
            repostBtn.setTitle(statusCellVM?.repostTitle, forState: .Normal)
            commentBtn.setTitle(statusCellVM?.commentTitle, forState: .Normal)
            supportBtn.setTitle(statusCellVM?.supportTitle, forState: .Normal)  
        }
    }
    
    // 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        // 准备界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}

// 设置微博下面的工具条上的按钮
extension HPToolBarView {

    private func setupUI() {
        
        repostBtn = addChildButton("转发", imageName: "timeline_icon_retweet")
        commentBtn = addChildButton("评论", imageName: "timeline_icon_comment")
        supportBtn = addChildButton("赞", imageName: "timeline_icon_like")
        
        // 约束
        repostBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.top.left.bottom.equalTo(self)
        })
        
        commentBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(repostBtn.snp_right)
            make.width.equalTo(repostBtn.snp_width)
        })
        
        supportBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(commentBtn.snp_width)
        })
        
        addSeparateLine()
    }
    

    // 添加按钮的方法
    private func addChildButton(title: String, imageName: String) -> UIButton {
        
        let btn = UIButton()
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(12)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        btn.sizeToFit()
        // 添加到界面
        addSubview(btn)
        return btn
    }
}


extension HPToolBarView {
    
    private func addSeparateLine() {
        
        // 添加分割线
        let lin1 = creatLineView()
        let lin2 = creatLineView()
        let width = 1 / UIScreen.mainScreen().scale
        let multiplied = 0.4
        addSubview(lin1)
        addSubview(lin2)
        
        // 设置约束
        lin1.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(repostBtn.snp_right)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            make.width.equalTo(width)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        lin2.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(supportBtn.snp_left)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            make.width.equalTo(width)
            make.centerY.equalTo(self.snp_centerY)
        }
    }
    
    
    private func creatLineView() -> UIView {
        
        let linView = UIView()
        linView.backgroundColor = UIColor.redColor()
        addSubview(linView)
        return linView
    }
}